package com.cat.chapter042;

/**
 * @author cat
 * @description
 * @create 2025/7/11 14:13
 * @since JDK17
 */

public class Solution04 {
    static char[] path = new char[100];

    // 使用red这三个拼一个n个长度的字符串，
    public static int f(int cur, int n) {
        int ans = 0;
        if (cur == n) { //
            return judge(n) == 1 ? 1 : 0;
        }
        path[cur] = 'r';
        ans += f(cur + 1, n);
        path[cur] = 'e';
        ans += f(cur + 1, n);
        path[cur] = 'd';
        ans += f(cur + 1, n);
        return ans;
    }

    public static int judge(int n) {
        int cnt = 0;
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                int s = i, e = j;
                for (s = i, e = j; s < e; s++, e--) {
                    if (path[s] != path[e]) {
                        break;
                    }
                }
                if (s >= e) {
                    cnt++;
                }
            }
        }
        return cnt;
    }

    public static int g(int n) {
        if (n < 4) {
            if (n <= 1) {
                return 0;
            }
            if (n == 2) {
                return 3;
            }
            if (n == 3) {
                return 18;
            }
        }

        return (n + 1) * 6;
    }

    public static void main(String[] args) {
        for (int i = 2; i < 10; i++) {
            System.out.println(i + " : " + f(0, i));
            System.out.println(i + " : " + g(i));
        }
    }
}
